package com.dpiinc.TechPage.pen.adapter

import android.content.Context

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.pen.fragment.ScanPenFragment
import com.dpiinc.TechPage.room.RoomAiWriterDatabase
import com.dpiinc.TechPage.room.bean.PenEntity
import com.dpiinc.TechPage.room.dao.PenDao
import com.beiji.lib.pen.model.PenInfo

/**
 * 设备列表Adapter
 * Created by X on 2018/6/14.
 */
class PenListAdapter(private val context: Context) : RecyclerView.Adapter<PenListAdapter.ViewHolder>() {

    private val deviceList = mutableListOf<PenInfo>()
    private var onItemInteractionListener: ScanPenFragment.OnListFragmentInteractionListener? = null

    private var penDao: PenDao = RoomAiWriterDatabase.getInstance(context).penDao();
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.fragment_pen, parent, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mItem = deviceList[position]
        holder.mNameView.text = deviceList[position].name
        holder.mAddressView.text = context?.resources?.getString(R.string.label_connect_pen_address, deviceList[position].macAddress)

        holder.itemView.setOnClickListener {
            onItemInteractionListener?.onListFragmentInteraction(deviceList[position])
        }
    }

    private var lastClickTime: Long = 0L
    fun isFastClick(): Boolean {
        var flag = false
        var curClickTime = System.currentTimeMillis()
        if (curClickTime - lastClickTime >= 1000) {
            flag = true
        }
        lastClickTime = curClickTime
        return flag
    }

    fun setOnItemInteractionListener(onItemInteractionListener: ScanPenFragment.OnListFragmentInteractionListener?) {
        this.onItemInteractionListener = onItemInteractionListener
    }

    override fun getItemCount(): Int {
        return deviceList.size
    }

    fun addPen(pen: PenInfo?): Boolean {
        if (pen == null) {
            return false
        }
        return if (isAdded(pen)) {
            val curPen = find(pen)
            curPen?.name = pen.name
            if(isFastClick()){
                notifyDataSetChanged()//1秒内刷新一次
            }
            false
        } else {

            val penEntity: PenEntity? = penDao.getPenByMacAddress(pen.macAddress)

            if(penEntity!= null){
                pen.name = penEntity.name;
            }
            deviceList.add(pen)
            notifyDataSetChanged()
            true
        }

    }

    fun hasPen(): Boolean {
        return itemCount != 0
    }

    private fun isAdded(pen: PenInfo?): Boolean {
        return find(pen) != null
    }

    private fun find(pen: PenInfo?): PenInfo? {
        return deviceList.find { pen?.macAddress == it.macAddress }
    }

    inner class ViewHolder(mView: View) : RecyclerView.ViewHolder(mView) {
        val mNameView: TextView = mView.findViewById(R.id.tv_name)
        val mAddressView: TextView = mView.findViewById(R.id.tv_address)
        val mConnectButton: TextView = mView.findViewById(R.id.btn_connect)
        var mItem: PenInfo? = null

        override fun toString(): String {
            return super.toString() + " '" + mAddressView.text + "'"
        }
    }
}
